<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 18-1-3
 * Time: 下午5:33
 */

namespace App\Http\Controllers\Api;


use App\Helpers\Jump;
use App\Http\Controllers\Controller;
use App\Repositories\Addons;
use Illuminate\Http\Request;

class UploadController extends Controller
{
    use Jump;

    /**
     * @param Request $request
     */
    public function index(Request $request)
    {
        $method = $request->get('action', '');

        $callback = $request->get('callback', '');

        try {
            $upload = new Addons\UploadRepository($method, $callback);

            if ($method == 'config') {
                $result = $upload->getConfig();

                return response()->json($result);
            }

            if (strpos($method, 'upload') !== false) {
                $result = $upload->upload();

                return response()->json($result);
            }

            if (strpos($method, 'list') !== false) {

                $size = $request->get('size', 20);

                $start = $request->get('start', 0);

                $result = $upload->lists($start, $size);

                return response()->json($result);
            }

        } catch (\Exception $exception) {
            echo $exception->getMessage();
        }


//        return htmlspecialchars($callback) . '(' . $result . ')';
    }
}